package IO;

import java.io.*;
//数据流：
//与机器无关的操作JAVA的基本数据类型
//读取的时候要跟写入的时候是一个顺序，否则可能会出现乱码
//writer int byte utf  reader int byte utf
public class Shujuliu {
    public static void main(String[] args) {
 dataoutStream();
 datainStream();
    }
    private static void datainStream(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\jkg.dat");
        try {
            InputStream is = new FileInputStream(file);
            BufferedInputStream bis = new BufferedInputStream(is);
            DataInputStream dateis = new DataInputStream(bis);
            int a = dateis.readInt();
            byte b = dateis.readByte();
            String s = dateis.readUTF();
            System.out.println(a+","+b+","+s);
            dateis.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }
    private static void dataoutStream(){
        File file = new File("C:\\Users\\Administrator\\Desktop\\File\\jkg.dat");
        try {
            OutputStream os = new FileOutputStream(file);
            BufferedOutputStream bos = new BufferedOutputStream(os);
            DataOutputStream dataos = new DataOutputStream(bos);
            dataos.writeInt(10); //占4个字节
            dataos.writeByte(1);//占1个字节
            dataos.writeUTF("中国");//一个中文占2-5个字节
            dataos.close();
        } catch (FileNotFoundException e) {
            e.printStackTrace();
        } catch (IOException e) {
            e.printStackTrace();
        }


    }
}
